<?php

require_once("SortUtils.php");
include_once("utils/DateUtils.php");

include_once("domain/AttributeDomain.php");
include_once("domain/MerchantDomain.php");
include_once("utils/CouponUtils.php");

class AlgorithmUtils extends SortUtils
{
	private $arrCostType= array();
	
	private $merFontMax	= 18;
	private $merFontMin	= 12;
	
	private $tagFontMax	= 22;
	private $tagFontMin	= 10;
	
	public function AlgorithmUtils()
	{		
	}
	
	
	public function calMerchantShowFont($oMercahnts)
	{
		//get local searches
		$arrSearches = array();
		foreach($oMercahnts as $oMerchant){
			$oAttribute = $oMerchant->getAttribute(1, 53);
			
			$localSearches = 0;	
			
			if(isset($oAttribute)){
				$localSearches = $oMerchant->getAttribute(1, 53)->getAttributeValue();
				
				if(empty($localSearches))
					$localSearches = 0;				
			}
			
			$arrSearches[] = $localSearches;
		}

		$datamax = max($arrSearches);
		$datamin = min($arrSearches);
		
		foreach($oMercahnts as $oMerchant){
			$fontSize 	= $this->merFontMin;
			
			$oAttribute = $oMerchant->getAttribute(1, 51);
			
			if(isset($oAttribute)){
				$cometition 	= $oMerchant->getAttribute(1, 51)->getAttributeValue();
				$localSearches 	= $oMerchant->getAttribute(1, 53)->getAttributeValue();
				
				$localSearches *= $cometition;
				
				if(!empty($localSearches)){
					$fontSize = $this->getSize($datamax, $datamin, $this->merFontMax, $this->merFontMin, $localSearches);
						
					if(empty($fontSize) || $fontSize < $this->merFontMin)
						$fontSize = $this->merFontMin;
					else if ($fontSize > $this->merFontMax)
						$fontSize = $this->merFontMax;
				}
			}
			
			if($oMerchant->getFeatured() == 1)
				$fontSize = $this->merFontMax;
			
			$oMerchant->setCfontSize($fontSize);
		}
	}
	
	public function calTagShowFont($oTags)
	{
		$arrSearches = array();
		foreach($oTags as $oTag){
			$oAttribute = $oTag->getAttribute(1, 53);
			
			if(isset($oAttribute)){
				$localSearches = $oTag->getAttribute(1, 53)->getAttributeValue();
				
				if(empty($localSearches))
					$localSearches = 0;
					
				$arrSearches[] = $localSearches;
			}
		}

		$datamax = max($arrSearches);
		$datamin = min($arrSearches);
		
		foreach($oTags as $oTag){
			$fontSize 	= $this->tagFontMin;
			
			$oAttribute = $oTag->getAttribute(1, 51);
			
			if(isset($oAttribute)){
				$cometition 	= $oTag->getAttribute(1, 51)->getAttributeValue();
				$localSearches 	= $oTag->getAttribute(1, 53)->getAttributeValue();
				
				$localSearches *= $cometition;
				
				if(!empty($localSearches)){
					$fontSize = $this->getSize($datamax, $datamin, $this->tagFontMax, $this->tagFontMin, $localSearches);
						
					if(empty($fontSize) || $fontSize < $this->tagFontMin)
						$fontSize = $this->tagFontMin;
					else if ($fontSize > $this->tagFontMax)
						$fontSize = $this->tagFontMax;
				}
			}
			
			if($oTag->getRrecommend() > 0)
				$fontSize = 26;
			
			$oTag->setCfontSize($fontSize);
		}
	}
	
	public function getCalTagPopular($oTags)
	{
		foreach($oTags as $oTag){
			$key = $this->getTagWeight($oTag);
			
			// calculate attributes, attribute weight max 5.
		}
	

		return $oTags;
	}
	
	public function getTagWeight($oTag)
	{
		$recommendWeight	= 5;
		
		$totalWeight		= 10;
		
		if($oTag->getRrecommend() > 0){
			$totalWeight += $recommendWeight;
		}
		
		
		
		return $totalWeight;
	}
	
	
	public function getFeaturedCoupon($oCoupons)
	{
		foreach($oCoupons as $oCoupon){
//			echo "-------------------original---------------------=============\n";
			
			$this->getCouponWeight($oCoupon);
			
//			echo "id : " . $oCoupon->getId() . "\n";
//			echo "name : " . $oCoupon->getName() . "\n";
//			echo "code : " . $oCoupon->getCode() . "\n";
//			echo "created date : " . $oCoupon->getCreateDate() . "\n";
//			echo "start date : " . $oCoupon->getStartDate() . "\n";
//			echo "getRsortValue : " . $oCoupon->getRsortValue() . "\n";	
		}
	
		usort($oCoupons, "AlgorithmUtils::sort_coupon_weight");
	
//		foreach($oCoupons as $oCoupon){
//			echo "-------------------sort---------------------------------\n";
//			echo "id : " . $oCoupon->getId() . "\n";
//			echo "name : " . $oCoupon->getName() . "\n";
//			echo "code : " . $oCoupon->getCode() . "\n";
//			echo "created date : " . $oCoupon->getCreateDate() . "\n";
//			echo "start date : " . $oCoupon->getStartDate() . "\n";
//			echo "getRsortValue : " . $oCoupon->getRsortValue() . "\n";
//			
//		}
		
		return $oCoupons;
	}
	
	public function getCouponWeight(&$oCoupon)
	{
		$cpcWeight 			= 30;	// cost type is CPC
		$cpaWeight 			= 20;	// cost type is CPA
		$cpmWeight 			= 10;	// cost type is CPM
		
		$trackingUrlWeight	= 10;	// has has tracking url
		$descriptWeight 	= 10;	// has descript
		$couponCodeWeight 	= 10;	// has coupon code
		
//		$hasPercentWeight	= 30;	// save off 10%
//		$hasDollarWeight 	= 30;	// save off $50
		
		$recommendedWeight 	= 30;	// is recommended 
		
		$totalWeight 		= 0;
		
		// 0:unknown, 1:CPA, 2:CPC, 3:CPM
		switch ($oCoupon->getCostType()) { 
			case 1: $totalWeight += $cpaWeight;
		  	case 2: $totalWeight += $cpcWeight;
		  	case 3: $totalWeight += $cpmWeight;
		}

		// 0:unknown, 1:google, 2:cj, 3:link share, 4:crawler, 5: share
		switch ($oCoupon->getSourceId()) {
			case 1: $totalWeight += 20;
		  	case 2: $totalWeight += 20;
		  	case 3: $totalWeight += 20;
		  	case 4: $totalWeight += 10;
		}
		
		$code 			= $oCoupon->getCode();
		$name 			= $oCoupon->getName();
		$description 	= $oCoupon->getDescript();
		$trackingURL 	= $oCoupon->getTrackingURL();
		$isRecommend 	= $oCoupon->getIsRecommend();
		
		if(!empty($isRecommend))
			$totalWeight += $recommendedWeight;
		
		if(!empty($trackingURL))
			$totalWeight += $trackingUrlWeight;
		
		if(!empty($code))
			$totalWeight += $couponCodeWeight;
		
		if(!empty($description))
			$totalWeight += $descriptWeight;
		
		$offPercent = CouponUtils::getOffPercent($name);
		if(is_numeric($offPercent)){
			$totalWeight += $offPercent;
		}
		
		$saveOff = CouponUtils::getSaveDollar($name);
		if(is_numeric($saveOff)){
			$totalWeight += $saveOff;
		}
		
//		echo "----------------------------\n";
//		echo "name: $name \n";
//		echo "per off: $offPercent \n";
//		echo "save off: $saveOff \n";
		
//		$pos = strpos($name, '$');
//		if ($pos !== false) {
//			$totalWeight += $hasDollarWeight;
//		}
//
//		$pos = strpos($name, '%');
//		if ($pos !== false) {
//			$totalWeight += $hasPercentWeight;
//		}
		
		$dateweight = 0;
		
//		$startDate = $oCoupon->getStartDate();
//		if(!empty($startDate)){
//			$dateweight = $this->getDateWeight($startDate);
//		}else{
//			$createDate = $oCoupon->getCreateDate();
//			if(!empty($createDate)){
//				$dateweight = $this->getDateWeight($createDate);
//			}
//		}
		
		$createDate = $oCoupon->getCreateDate();
		if(empty($createDate)){
			$startDate = $oCoupon->getStartDate();
			if(!empty($startDate)){
				$dateweight = $this->getDateWeight($startDate);
			}
		}else{
			$dateweight = $this->getDateWeight($createDate);
		}
		
//		echo $oCoupon->getId() . "********* coupon id, total weight: $totalWeight \n";
		
		$totalWeight += $dateweight;
		
//		echo $oCoupon->getId() . " coupon id, date weight: $dateweight \n";
		
		$oCoupon->setRsortValue($totalWeight);
		
		return $totalWeight;
	}
	
	/* scale calculate
	 * 
	 */
	function getFontSize($datamax, $datamin, $fontmax, $fontmin, $datavalue)
	{
		if($datamax - $datamin == 0)
			return 0;
		
		$datascale = ($datavalue - $datamin) / ($datamax - $datamin);
		
		$rs = $fontmin + ($fontmax - $fontmin) * $datascale;
		
		return round ($rs);
	}
	
	/* log calculate
	 * 
	 */
	function getSize($datamax, $datamin, $fontmax, $fontmin, $datavalue)
	{
		$rs = (log($datavalue) - log($datamin)) / (log($datamax) - log($datamin)) * ($fontmax - $fontmin) + $fontmin;
		
		return $rs;
	}
	
	function getDateWeight($date)
	{
		$days = DateUtils::getNowDays($date);
		
//		echo "-----======date : $date \n";
//		echo "-----days : $days \n";
		
		if($days == 0){
			return 10000;
		}else if($days > 0){
			return 10000 - $days * 100;
		}else{
			return 10000 + $days * 100;
		}
		
	}
	
	
	public static function sort_coupon_weight($a, $b)
	{
	    if ($a->getRsortValue() == $b->getRsortValue()) {
	        return 0;
	    }
	    
	    return ($a->getRsortValue() < $b->getRsortValue()) ? -1 : 1;
	}
	
}

?>